﻿@using BlazorDatasheet.Edit;
@using BlazorDatasheet.Util
@using BlazorDatasheet.Core.Util
@using BlazorDatasheet.Core.Interfaces
@using BlazorDatasheet.Core.Data
@using BlazorDatasheet.Core.Edit

@inherits BaseEditor

<textarea @oninput="HandleOnChange"
          class="text-input"
          style="@CssUtil.GetStyledInput(Cell); z-index: 1000;"
          rows="@RowsCount"
          @ref="InputRef">@CurrentValue</textarea>

@code {
    public static int RowsCount = 5;

    private IReadOnlyCell Cell;

    public override void BeforeEdit(IReadOnlyCell cell, Sheet sheet)
    {
        Cell = cell;
    }

    public override void BeginEdit(EditEntryMode entryMode, string? editValue, string key)
    {
        CurrentValue = editValue?.ToString();
        if (CurrentValue == null)
            CurrentValue = string.Empty;

        if (entryMode == EditEntryMode.Key && !String.IsNullOrEmpty(key) && key.Length == 1)
        {
            CurrentValue = key;
        }
    }

    private void HandleOnChange(ChangeEventArgs args)
    {
        CurrentValue = args.Value.ToString();
    }

    public override bool HandleKey(string key, bool ctrlKey, bool shiftKey, bool altKey, bool metaKey)
    {
        if (!ctrlKey && key == "Enter")
        {
            return true;
        }

        if (KeyUtil.IsArrowKey(key))
        {
            return true;
        }

        return base.HandleKey(key, ctrlKey, shiftKey, altKey, metaKey);
    }

}